package com.zjg.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_noticeType;
import com.common.entity.A_notices;
import com.common.entity.A_online;
import com.common.utils.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjg.dao.NoticeTypeDao;
import com.zjg.dao.impl.NoticeTypeDaoImpl;
import com.zjg.dao.impl.NoticesDaoImpl;
import com.zjg.dao.impl.OnlineDaoImpl;

public class NoticeTypeServlet extends HttpServlet {
	private NoticeTypeDao ntdao=new NoticeTypeDaoImpl();
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr=request.getParameter("opr");
		if("add".equals(opr)){
			add(request, resp);
		}else if("update".equals(opr)){
			update(request, resp);
		}else if("del".equals(opr)){
			delete(request, resp);
		}else{
			list(request, resp);
		}
	}
	
	
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		List<A_noticeType> list=ntdao.findByCondition(null);
		request.setAttribute("typelist", list);
		for(A_noticeType ntype:list)
		{
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("noticeType_id", ntype.getNoticeType_id());
			List<A_notices> noticeslist=new NoticesDaoImpl().findByCondition(map);
			ntype.setSize(noticeslist==null?0:noticeslist.size());
		}
		if(request.getParameter("root")!=null)
		{
			ObjectMapper mapper=new ObjectMapper();
			String json=mapper.writeValueAsString(list);
			
			PrintWriter out=resp.getWriter();
			out.print(json);
		}
		else
		{
			String message=(String) request.getAttribute("excuteMsg_noticeType");
			if(message!=null&&message.length()>0)
			{
				request.getRequestDispatcher("noticeType_list.jsp").forward(request, resp);
			}
			else
			{
				request.getSession().setAttribute("excuteMsg_noticeType", "");
				request.getRequestDispatcher("noticeType_list.jsp").forward(request, resp);
			}
		}
	}
	
	private void update(HttpServletRequest request, HttpServletResponse resp)
			throws UnsupportedEncodingException, IOException, ServletException {
		//2、判断数据的有效性
		//3、获取数据
		int rows=0;
		String errMsg="";
		try{
			Integer noticeType_id=StringUtils.str2Integer(request.getParameter("noticeType_id"), null);
			A_noticeType a_noticeType=null;
			if(noticeType_id!=null){
				a_noticeType=ntdao.findById(noticeType_id);
				if(a_noticeType==null) throw new RuntimeException("找不到该记录："+noticeType_id);
				//4、保存数据
				a_noticeType.setName(request.getParameter("typename"));
				rows=ntdao.updateNoticeType(a_noticeType);
			}else{
				throw new RuntimeException("无该条记录");
			}
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		if(rows>0){
			request.setAttribute("excuteMsg_noticeType", "修改成功");
			request.getSession().setAttribute("excuteMsg_noticeType", "修改成功");
		}else{
			request.getSession().setAttribute("excuteMsg_noticeType", errMsg);
		}
		request.getRequestDispatcher("noticeTypeServlet?opr=list").forward(request, resp);
	}

	private void add(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		//2、判断数据的有效性
		//3、获取数据
		A_noticeType noticeType=new A_noticeType();
		int rows=0;
		String errMsg="";
		try{
			noticeType.setName(request.getParameter("typename"));
			//4、保存数据
			Object[] results=(Object[]) ntdao.saveNoticeType(noticeType);
			rows=(Integer) results[0];
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		if(rows>0){
			request.setAttribute("excuteMsg_noticeType", "新增成功");
			request.getSession().setAttribute("excuteMsg_noticeType", "新增成功");
		}else{
			request.getSession().setAttribute("excuteMsg_noticeType", errMsg);
		}
		request.getRequestDispatcher("noticeTypeServlet?opr=list").forward(request, resp);
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		int rows=0;
		String errMsg="";
		
		String del_list=request.getParameter("del_list");
		if(del_list!=null&&del_list.length()>0)
		{
			String[] delArrStr=del_list.split(";");
			List<Integer> delArrInt=new ArrayList<Integer>();
			for(int i=0;i<delArrStr.length;i++)
			{
				delArrInt.add(StringUtils.str2Integer(delArrStr[i], null));
			}
			for(Integer i:delArrInt)
			{
				rows=ntdao.deleteNoticeType(i);
			}
		}
		else{
			try{
				Integer noticeType_id=StringUtils.str2Integer(request.getParameter("noticeType_id"), null);
				if(noticeType_id!=null){
					//4、保存数据
					rows=ntdao.deleteNoticeType(noticeType_id);
				}else{
					throw new RuntimeException("该记录不存在");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
		}
		if(rows>0){
			request.setAttribute("excuteMsg_noticeType", "删除成功");
			request.getSession().setAttribute("excuteMsg_noticeType", "删除成功");
		}else{
			request.getSession().setAttribute("excuteMsg_noticeType", errMsg);
		}
		request.getRequestDispatcher("noticeTypeServlet?opr=list").forward(request, resp);
	}

	

}
